import 'package:get/get.dart';
import 'package:vape_xe/routes/app_pages.dart';
import 'package:vape_xe/services/services.dart';
import 'package:vape_xe/utils/utils.dart';
import 'package:vape_xe/widgets/widgets.dart';

class WithdrawAddressController extends GetxController {
  /// 响应式成员变量
  final addressList = [].obs;

  /// 事件
  void newAddress() {
    Get.toNamed(Routes.WITHDRAWADDRNEW, arguments: Get.arguments)?.then((result) {
      if (result == true) {
        requestAddressList();
      }
    });
  }

  void onTap(int index) {
    Get.bottomSheet(
      WithdrawConfirmView(params: {
        'money': Get.arguments['money'],
        'bandId': addressList[index]['id'],
        'uniqueId': addressList[index]['uniqueId'], //地址
        'type': addressList[index]['type'],
      }),
      isScrollControlled: true,
    ).then((result) {
      //提现id
      if (result != null) {
        var type = (UserStore.to.profile.email?.isEmpty ?? true) ? 0 : 1; // 0手机 1Email
        Get.toNamed(Routes.WITHDRAWCODE,
            arguments: {
              'cashId': result,
              'type': type,
              'email': UserStore.to.profile.email,
              'mobile': UserStore.to.profile.mobile,
              'mobileType': UserStore.to.profile.mobileType,
            }..removeWhere((key, value) =>
                value == null ||
                (type == 0 && key == 'email') ||
                (type == 1 && (key == 'mobile' || key == 'mobileType'))));
      }
    });
  }

  Future<void> requestAddressList() async {
    await Request().get('/userBand/getList', {
      'type': Get.arguments['type'],
    }).then((result) {
      addressList.clear();
      addressList.addAll(result['result']);
    });
  }

  /// 生命周期
  @override
  void onReady() {
    super.onReady();
    requestAddressList();
  }
}
